package com.itheima.springbootcondition.condition;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.List;
import java.util.Map;

/**
 * @author wuli
 * @create 2021-10-21 22:14
 */
public class ClassCondition implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

        Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(ConditionOnClass.class.getName());
        metadata.getAnnotationAttributes(ConditionOnClass.class.getName());
        String[] values = (String[]) annotationAttributes.get("value");
        try {
            for (String value : values) {
                Class.forName(value);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return  false;
        }
        return true;
    }
}
